<template>
  <div class="user-home">
    <!-- 用户欢迎区域 -->
    <div class="welcome-section">
      <div class="container">
        <div class="welcome-content">
          <div class="user-info">
            <div class="user-avatar">
              <span>{{ userStore.nickname.charAt(0) }}</span>
            </div>
            <div class="user-details">
              <h2>欢迎回来，{{ userStore.nickname }}</h2>
              <p>{{ userStore.userInfo?.phone || '未绑定手机' }}</p>
            </div>
          </div>
          <div class="quick-stats">
            <div class="stat-item">
              <div class="stat-number">{{ userStore.orderStats.total }}</div>
              <div class="stat-label">总订单</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">{{ userStore.points }}</div>
              <div class="stat-label">积分</div>
            </div>
            <div class="stat-item">
              <div class="stat-number">{{ userStore.unreadCount }}</div>
              <div class="stat-label">未读消息</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷功能区域 -->
    <div class="quick-actions">
      <div class="container">
        <h3>快捷功能</h3>
        <div class="action-grid">
          <router-link to="/orders" class="action-card">
            <div class="action-icon">📋</div>
            <h4>我的订单</h4>
            <p>查看和管理订单</p>
          </router-link>
          <router-link to="/messages" class="action-card">
            <div class="action-icon">💬</div>
            <h4>消息中心</h4>
            <p>查看系统消息</p>
          </router-link>
          <router-link to="/profile" class="action-card">
            <div class="action-icon">👤</div>
            <h4>个人中心</h4>
            <p>管理个人信息</p>
          </router-link>
          <router-link to="/account" class="action-card">
            <div class="action-icon">💰</div>
            <h4>账户中心</h4>
            <p>查看余额和积分</p>
          </router-link>
        </div>
      </div>
    </div>

    <!-- 推荐服务区域 -->
    <div class="recommended-services">
      <div class="container">
        <div class="section-header">
          <h3>推荐服务</h3>
          <router-link to="/services" class="view-all">查看更多</router-link>
        </div>
        <div class="services-grid">
          <div class="service-card" @click="goToService('衣物洗护')">
            <div class="service-icon">👔</div>
            <h4>衣物洗护</h4>
            <p>日常衣物专业洗护</p>
            <span class="price">起价 ¥15</span>
          </div>
          <div class="service-card" @click="goToService('鞋类清洗')">
            <div class="service-icon">👟</div>
            <h4>鞋类清洗</h4>
            <p>运动鞋皮鞋专业清洗</p>
            <span class="price">起价 ¥25</span>
          </div>
          <div class="service-card" @click="goToService('家纺洗护')">
            <div class="service-icon">🛏️</div>
            <h4>家纺洗护</h4>
            <p>床品窗帘地毯洗护</p>
            <span class="price">起价 ¥35</span>
          </div>
          <div class="service-card" @click="goToService('奢侈品护理')">
            <div class="service-icon">👜</div>
            <h4>奢侈品护理</h4>
            <p>名牌包包皮具护理</p>
            <span class="price">起价 ¥80</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 最近订单 -->
    <div class="recent-orders" v-if="recentOrders.length > 0">
      <div class="container">
        <div class="section-header">
          <h3>最近订单</h3>
          <router-link to="/orders" class="view-all">查看全部</router-link>
        </div>
        <div class="orders-list">
          <div
            v-for="order in recentOrders"
            :key="order.id"
            class="order-item"
            @click="goToOrderDetail(order.id)"
          >
            <div class="order-info">
              <div class="order-number">{{ order.orderNumber }}</div>
              <div class="order-status">
                <el-tag :type="getStatusType(order.status)" size="small">
                  {{ getStatusText(order.status) }}
                </el-tag>
              </div>
            </div>
            <div class="order-details">
              <div class="service-name">{{ order.serviceName }}</div>
              <div class="order-time">{{ formatDate(order.createdAt) }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()
const recentOrders = ref([])

// 获取最近订单
const fetchRecentOrders = async () => {
  try {
    // 这里应该调用API获取最近订单
    // 暂时使用模拟数据
    recentOrders.value = [
      {
        id: 1,
        orderNumber: 'ORD20241201001',
        serviceName: '衣物洗护',
        status: 'completed',
        createdAt: new Date(Date.now() - 24 * 60 * 60 * 1000)
      },
      {
        id: 2,
        orderNumber: 'ORD20241202001',
        serviceName: '鞋类清洗',
        status: 'in_service',
        createdAt: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000)
      }
    ]
  } catch (error) {
    console.error('获取最近订单失败:', error)
  }
}

// 跳转到服务页面
const goToService = (serviceName) => {
  router.push(`/services?category=${encodeURIComponent(serviceName)}`)
}

// 跳转到订单详情
const goToOrderDetail = (orderId) => {
  router.push(`/orders/${orderId}`)
}

// 获取状态类型
const getStatusType = (status) => {
  const statusMap = {
    pending: 'warning',
    paid: 'primary',
    in_service: 'info',
    completed: 'success',
    cancelled: 'danger'
  }
  return statusMap[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    pending: '待支付',
    paid: '已支付',
    in_service: '服务中',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statusMap[status] || status
}

// 格式化日期
const formatDate = (date) => {
  return new Date(date).toLocaleDateString('zh-CN')
}

onMounted(() => {
  fetchRecentOrders()
})
</script>

<style scoped>
.user-home {
  min-height: 100vh;
  background: #f5f7fa;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 欢迎区域 */
.welcome-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 40px 0;
}

.welcome-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-avatar {
  width: 80px;
  height: 80px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  font-weight: bold;
}

.user-details h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
}

.user-details p {
  margin: 0;
  opacity: 0.8;
}

.quick-stats {
  display: flex;
  gap: 40px;
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  opacity: 0.8;
}

/* 快捷功能 */
.quick-actions {
  padding: 40px 0;
  background: white;
}

.quick-actions h3 {
  margin-bottom: 30px;
  color: #333;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.action-card {
  display: block;
  padding: 30px;
  background: #f8f9fa;
  border-radius: 12px;
  text-decoration: none;
  color: inherit;
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.action-card:hover {
  background: white;
  border-color: #409eff;
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.15);
}

.action-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.action-card h4 {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 18px;
}

.action-card p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

/* 推荐服务 */
.recommended-services {
  padding: 40px 0;
  background: #f8f9fa;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.section-header h3 {
  margin: 0;
  color: #333;
}

.view-all {
  color: #409eff;
  text-decoration: none;
  font-weight: 500;
}

.view-all:hover {
  text-decoration: underline;
}

.services-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.service-card {
  background: white;
  padding: 30px;
  border-radius: 12px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.service-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.service-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.service-card h4 {
  margin: 0 0 8px 0;
  color: #333;
}

.service-card p {
  margin: 0 0 16px 0;
  color: #666;
  font-size: 14px;
}

.price {
  color: #f56c6c;
  font-weight: bold;
  font-size: 16px;
}

/* 最近订单 */
.recent-orders {
  padding: 40px 0;
  background: white;
}

.orders-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.order-item {
  background: #f8f9fa;
  padding: 20px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.3s ease;
}

.order-item:hover {
  background: #e9ecef;
}

.order-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.order-number {
  font-weight: 500;
  color: #333;
}

.order-details {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.service-name {
  color: #666;
}

.order-time {
  color: #999;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .welcome-content {
    flex-direction: column;
    gap: 20px;
    text-align: center;
  }
  
  .quick-stats {
    gap: 20px;
  }
  
  .action-grid {
    grid-template-columns: 1fr;
  }
  
  .services-grid {
    grid-template-columns: 1fr;
  }
}
</style>
